package com.fare.taluo.core.domain.config.city;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.dayatang.domain.Entity;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.domain.QuerySettings;
import com.fare.taluo.base.ddd.domain.baseentity.BaseEntity;
import com.fare.taluo.vo.core.PortEntityVO;
/**
 * 机场信息
 * <p>一个机场对应一个城市
 * @author xiangsf 2014-3-19
 *
 */
@javax.persistence.Entity
@Table(name = "FDC_PORT")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class PortEntity extends BaseEntity  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6417631815978996171L;
	/**
	 * 机场三字码
	 */
	@Column(name = "CODE")
	private String code;
	/**
	 * 机场四字码
	 */
	@Column(name = "FOUR_CODE")
	private String fourCode;
	/**
	 * 机场名称
	 */
	@Column(name = "name")
	private String name;
	/**
	 * 机场别名
	 */
	@Column(name = "alias_name")
	private String aliasName;
	/**
	 * 热门等级
	 */
	@Column(name="HOT_LEVEL")
	private Integer hotlevel;
	/**
	 * 创建时间
	 */
	@Column(name = "CREATE_TIME")
	private Date createTime = new Date();
	/**
	 * 最后更新时间
	 */
	@Column(name = "UPDATE_TIME")
	private Date updateTime = new Date();
	/**
	 * 所属城市ID
	 */
	@Column(name ="CITY_ID", insertable=false, updatable=false)
	private Long cityId;
	/**
	 * 所属城市名称
	 */
	@Column(name = "CITY_NAME")
	private String cityName;
	/**
	 * 排序号
	 */
	@Column(name = "PRIORITY")
	private Integer priority = 0;
	/**
	 * 所属城市对象
	 */
	@JoinColumn(name = "CITY_ID", referencedColumnName = "ID")
	@ManyToOne
	@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
	private CityEntity city;
	
	public void loadByVo(PortEntityVO vo){
		
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getFourCode() {
		return fourCode;
	}

	public void setFourCode(String fourCode) {
		this.fourCode = fourCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public String getAliasName() {
		return aliasName;
	}

	public void setAliasName(String aliasName) {
		this.aliasName = aliasName;
	}

	public CityEntity getCity() {
		return city;
	}


	public void setCity(CityEntity city) {
		this.city = city;
	}


	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Integer getHotlevel() {
		return hotlevel;
	}

	public void setHotlevel(Integer hotlevel) {
		this.hotlevel = hotlevel;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	
	public Long getCityId() {
		return cityId;
	}

	public void setCityId(Long cityId) {
		this.cityId = cityId;
	}

	private static PortEntityRepository getPortEntityRepository(){
		return InstanceFactory.getInstance(PortEntityRepository.class);
	}

	public PortEntity findByPortCode(String portCode){
		return getPortEntityRepository().findByPortCode(portCode);
	}
	
	public List<PortEntity> findAll(){
		return getPortEntityRepository().findAll();
	}
	
	public Map<String,PortEntity> findPortEntityToMap(){
		List<PortEntity> list = this.findAll();
		Map<String, PortEntity> map = new HashMap<String, PortEntity>();
		for (PortEntity p : list) {
			map.put(p.getCode(), p);
		}	
		return map;
	}
	
	public Map<String,PortEntity> findPortEntityToMapByName(){
		List<PortEntity> list = this.findAll();
		Map<String, PortEntity> map = new HashMap<String, PortEntity>();
		for (PortEntity p : list) {
			map.put(p.getCityName(), p);
		}	
		return map;
	}
	
	public Map<String,PortEntity> findPortEntityToMapByPortName(){
		List<PortEntity> list = this.findAll();
		Map<String, PortEntity> map = new HashMap<String, PortEntity>();
		for (PortEntity p : list) {
			map.put(p.getName().substring(0, 2), p);
		}	
		return map;
	}
	
	public List<PortEntity> findPortEntityByCityCode(String cityCode) {
		return getPortEntityRepository().findByCityCode(cityCode);
	}
	public List<PortEntity> findPortEntityByCityName(String cityName){
		return getPortEntityRepository().findPortEntityByCityName(cityName);
	}
	public List<PortEntity> findPortEntityByPortName(String portName){
		return getPortEntityRepository().findPortByPortName(portName);
	}
	
	@Override
	public String toString() {
		return "PortEntity [code=" + code + ", fourCode=" + fourCode
				+ ", name=" + name + ", aliasName=" + aliasName + ", hotlevel="
				+ hotlevel + ", createTime=" + createTime + ", updateTime="
				+ updateTime + ", cityId=" + cityId + ", cityName=" + cityName
				+ ", priority=" + priority + "]";
	}
}
